package com.ropeok.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ropeok.domain.ResponseResult;
import com.ropeok.domain.User;
import com.ropeok.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @BelongsProject: training-mission
 * @BelongsPackage: com.ropeok.controller
 * @Author: zhofeifan
 * @CreateTime: 2022-09-26  14:25
 * @Description: 用户登录
 * @Version: 1.0
 */
@RestController
public class ManageController {
    @Autowired
    private UserService userService;
    @GetMapping("/user/{id}")
    @PreAuthorize("hasAuthority('system:test:list')")
    public ResponseResult findById(@PathVariable("id") Long id){
       User user =  userService.findById(id);
       if (user == null){
           return new ResponseResult(500,"没有该用户");
       }
       return new ResponseResult(200,"查询成功",user);
    }

    @GetMapping("/user")
    @PreAuthorize("hasAuthority('system:test:list')")
    public ResponseResult findAll(){
         List<User> list =  userService.findAll();
        return new ResponseResult(200,"查询成功",list);
    }
    @GetMapping("/user/{pageSize}/{pageNumber}")
    @PreAuthorize("hasAuthority('system:test:list')")
    public ResponseResult findByPage(@PathVariable("pageSize") Integer pageSize,@PathVariable("pageNumber")Integer pageNumber){
           Page page =  userService.findByPage(pageSize,pageNumber);
           return new ResponseResult(200,"查询成功",page);
    }
    @PostMapping("/user")
    @PreAuthorize("hasAuthority('system:dept:list')")
    public ResponseResult insertUser(@RequestBody User user){
        userService.insertUser(user);
        return new ResponseResult(200,"插入成功");
    }
    @DeleteMapping ("/user/{id}")
    @PreAuthorize("hasAuthority('system:dept:list')")
    public ResponseResult deleteUserById(@PathVariable("id") Integer id){
        userService.deleteUserById(id);
        return new ResponseResult(200,"删除成功");
    }
    @PutMapping("/user")
    @PreAuthorize("hasAuthority('system:dept:list')")
    public ResponseResult updateUser(@RequestBody User user){
        userService.updateUser(user);
        return new ResponseResult(200,"更新成功");
    }
}
